import 'package:common/common.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/module/world/audio/recorder/recorder_manager.dart';

import 'audio_card_record_state.dart';
import 'audio_record_config.dart';

/// @description:
/// @author
/// @date: 2025-01-20 09:58:27
class AudioCardRecordLogic extends GetxController {
  final state = AudioCardRecordState();

  @override
  void onInit() {
    super.onInit();
    fetchReocrdConfig();
  }

  void fetchReocrdConfig() {
    HttpProvider.get('/user/getEditVoiceInit', explain: '获取录制声音卡片初始化数据', success: (result) {
      AudioRecordConfigData config = AudioRecordConfigData.fromJson(result.data);
      state.audioRecordConfig = config;
      for (RecordAudioBtn element in config.editArea?.btns ?? []) {
        if (element.key == 'voiceRecord') {
          state.recordAudioBtn = element;
          String minlong = element.click?.params?.value?.minlong ?? '10';
          String maxlong = element.click?.params?.value?.maxlong ?? '120';
          state.minlong = minlong.toInt();
          state.maxlong = maxlong.toInt();
          RecorderManager.instance.configRecordParams(minTime: state.minlong, maxTime: state.maxlong);
        }
      }
      update();
    });
  }

  void submitReocrdResult({required String audioPath, required String timelong}) {
    Toast.showLoading();
    HttpProvider.upload(
      '/user/subEditVoiceCard',
      paths: {'voice': audioPath},
      params: {
        'timelong': timelong,
      },
      explain: '提交编辑声音卡片',
      send: (count, total) {
        logs('------------上传进度: $count/$total    -----   ${count / total * 100}%');
      },
      success: (result) {
        Toast.dismiss();

        Toast.showText(result.message);
        fetchReocrdConfig();
        update();
      },
      failure: (result) {
        Toast.showText(result.message);
      },
    );
  }

  void deleteAudioResult() {
    HttpProvider.post(
      '/user/subDelVoiceCard',
      explain: '删除声音卡片',
      success: (result) {
        Toast.showText(result.message);
        fetchReocrdConfig();
      },
      failure: (result) {
        Toast.showText(result.message);
      },
    );
  }
}
